/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.ega.sgces.sgcespos;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import org.apache.log4j.Logger;
import pe.com.ega.sgces.dao.ClienteDaoImpl;
import pe.com.ega.sgces.dao.DespachoDaoImpl;
import pe.com.ega.sgces.dao.TransaccionDaoImpl;
import pe.com.ega.sgces.logic.ClienteLogica;
import pe.com.ega.sgces.logic.ClienteLogicaImpl;
import pe.com.ega.sgces.logic.DespachoLogica;
import pe.com.ega.sgces.logic.DespachoLogicaImpl;
import pe.com.ega.sgces.logic.TransaccionLogica;
import pe.com.ega.sgces.logic.TransaccionLogicaImpl;
import pe.com.ega.sgces.model.Cliente;
import pe.com.ega.sgces.model.Despacho;

/**
 *
 * @author FLOPEZ
 */
public class ClienteFrame extends JFrame {

    private final static Logger logger = Logger.getLogger(ClienteFrame.class);
    
    private ClienteLogica clienteLogica;
    private TransaccionLogica transaccionLogica;
    private DespachoLogica despachoLogica;

    public ClienteFrame(Despacho despacho, ClienteLogica clienteLogica, TransaccionLogica transaccionLogica, DespachoLogica despachoLogica) {
        initComponents();
        this.clienteLogica = clienteLogica;
        this.transaccionLogica = transaccionLogica;
        this.despachoLogica = despachoLogica;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jrucCliente = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jrazonCliente = new javax.swing.JTextField();
        Registrar = new javax.swing.JButton();
        cancelar = new javax.swing.JButton();

        setResizable(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
        jLabel1.setText("RAZON SOCIAL :");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(30, 110, 210, 29);

        jrucCliente.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
        jrucCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrucClienteActionPerformed(evt);
            }
        });
        jrucCliente.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jrucClienteFocusGained(evt);
            }
        });
        jPanel1.add(jrucCliente);
        jrucCliente.setBounds(240, 50, 170, 40);

        jLabel2.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
        jLabel2.setText("R.U.C               :");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(30, 50, 210, 29);

        jrazonCliente.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
        jPanel1.add(jrazonCliente);
        jrazonCliente.setBounds(240, 110, 380, 40);

        Registrar.setBackground(new java.awt.Color(133, 238, 75));
        Registrar.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
        Registrar.setForeground(new java.awt.Color(51, 51, 51));
        Registrar.setMnemonic(KeyEvent.VK_A);
        Registrar.setText("ACEPTAR");
        Registrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RegistrarActionPerformed(evt);
            }
        });
        jPanel1.add(Registrar);
        Registrar.setBounds(90, 190, 200, 80);

        cancelar.setBackground(new java.awt.Color(255, 62, 79));
        cancelar.setFont(new java.awt.Font("Lucida Sans", 0, 24)); // NOI18N
        cancelar.setForeground(new java.awt.Color(51, 51, 51));
        cancelar.setMnemonic(KeyEvent.VK_C);
        cancelar.setText("CANCELAR");
        cancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarActionPerformed(evt);
            }
        });
        jPanel1.add(cancelar);
        cancelar.setBounds(340, 190, 180, 80);

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void jrucClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrucClienteActionPerformed
    }//GEN-LAST:event_jrucClienteActionPerformed

    private void jrucClienteFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jrucClienteFocusGained
        jrucCliente.setText("");
        jrazonCliente.setText("");
    }//GEN-LAST:event_jrucClienteFocusGained

    private void RegistrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RegistrarActionPerformed

        Cliente temporal1 = new Cliente();
        temporal1.setNumerodocumento(jrucCliente.getText());
        temporal1.setRazonsocial(jrazonCliente.getText());
        if (clienteLogica.buscarPorCodigo(jrucCliente.getText()) != null) {
            JOptionPane.showMessageDialog(null, "Cliente Ya existe", "Error", JOptionPane.ERROR_MESSAGE);
        } else {
            if (jrucCliente.getText().isEmpty() || jrazonCliente.getText().isEmpty()) {
                JOptionPane.showMessageDialog(null, "Faltan Datos", "Error", JOptionPane.ERROR_MESSAGE);
            } else {
                try {
                    clienteLogica.grabar(temporal1);
                    limpiar();
                    salir(evt);
                } catch (Exception ex) {
                    logger.error("Mensaje:\n" + ex.getMessage());
                    JOptionPane.showMessageDialog(null, "Error al Grabar", "Error", JOptionPane.ERROR_MESSAGE);
                }
            }
        }


    }//GEN-LAST:event_RegistrarActionPerformed

    private void cancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarActionPerformed
        salir(evt);
    }//GEN-LAST:event_cancelarActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton Registrar;
    private javax.swing.JButton cancelar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jrazonCliente;
    private javax.swing.JTextField jrucCliente;
    // End of variables declaration//GEN-END:variables

    private void salir(java.awt.event.ActionEvent evt) {
        actionPerformed(evt);
    }

    private void actionPerformed(ActionEvent evt) {
        setVisible(false);
        dispose();
    }

    public void limpiar() {
        jrucCliente.setText("");
        jrazonCliente.setText("");
    }
}
